package com.qidian.tcba.admin.service.impl;


import com.qidian.tcba.admin.model.dto.assignTask.AssignTaskDTO;
import com.qidian.tcba.admin.model.dto.assignTask.AssignTaskItemDTO;
import com.qidian.tcba.admin.model.dto.attachment.AttachmentBaseInfoDTO;
import com.qidian.tcba.admin.model.dto.auditRecord.AuditRecordDTO;
import com.qidian.tcba.admin.model.dto.auditRecord.ReformIdeaDTO;
import com.qidian.tcba.admin.model.dto.recordProcess.AuditRecordDetailsDTO;
import com.qidian.tcba.admin.model.entity.*;
import com.qidian.tcba.admin.model.utils.InformUtils;
import com.qidian.tcba.admin.service.ParkLotRecordAssignTaskItemService;
import com.qidian.tcba.admin.service.ParkLotRecordAuditRecordService;
import com.qidian.tcba.dao.*;
import com.qidian.tcba.utils.BeanMapper;
import com.qidian.tcba.utils.TablesName;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 停车场备案审核记录表 服务实现类
 * </p>
 *
 * @author 冯永龙
 * @since 2021-07-06
 */
@Service
public class ParkLotRecordAuditRecordServiceImpl implements ParkLotRecordAuditRecordService {

    private final AnnouncementMapper announcementMapper;

    private final ParkLotMapper parkLotMapper;

    private final ParkLotRecordAuditRecordMapper parkLotRecordAuditRecordMapper;

    private final AttachmentMapper attachmentMapper;

    private final ParkLotRecordAssignTaskMapper parkLotRecordAssignTaskMapper;

    private final ParkLotRecordAssignTaskItemService parkLotRecordAssignTaskItemService;

    private final ParkLotRecordTaskMapper parkLotRecordTaskMapper;

    private final ParkLotRecordProcessHistoryMapper parkLotRecordProcessHistoryMapper;

    private final ParkLotRecordTaskLastChangeMapper parkLotRecordTaskLastChangeMapper;

    public ParkLotRecordAuditRecordServiceImpl(AnnouncementMapper announcementMapper,
                                               ParkLotMapper parkLotMapper, ParkLotRecordAuditRecordMapper parkLotRecordAuditRecordMapper,
                                               AttachmentMapper attachmentMapper,
                                               ParkLotRecordAssignTaskMapper parkLotRecordAssignTaskMapper,
                                               ParkLotRecordAssignTaskItemService parkLotRecordAssignTaskItemService,
                                               ParkLotRecordTaskMapper parkLotRecordTaskMapper,
                                               ParkLotRecordProcessHistoryMapper parkLotRecordProcessHistoryMapper,
                                               ParkLotRecordTaskLastChangeMapper parkLotRecordTaskLastChangeMapper) {
        this.announcementMapper = announcementMapper;
        this.parkLotMapper = parkLotMapper;
        this.parkLotRecordAuditRecordMapper = parkLotRecordAuditRecordMapper;
        this.attachmentMapper = attachmentMapper;
        this.parkLotRecordAssignTaskMapper = parkLotRecordAssignTaskMapper;
        this.parkLotRecordAssignTaskItemService = parkLotRecordAssignTaskItemService;
        this.parkLotRecordTaskMapper = parkLotRecordTaskMapper;
        this.parkLotRecordProcessHistoryMapper = parkLotRecordProcessHistoryMapper;
        this.parkLotRecordTaskLastChangeMapper = parkLotRecordTaskLastChangeMapper;
    }

    @Override
    public List<AuditRecordDetailsDTO> getAuditRecordDetails(int processHistoryId) {
        //获取审核详情列表
        List<AuditRecordDetailsDTO> auditRecordList = parkLotRecordAuditRecordMapper.getAuditRecords(processHistoryId);
        //设置附件
        for (AuditRecordDetailsDTO auditRecords : auditRecordList) {
            List<Attachment> attachments = attachmentMapper.getAttachments("ParkLotRecordAuditRecord", auditRecords.getId());
            auditRecords.setAttachments(BeanMapper.mapList(attachments,AttachmentBaseInfoDTO.class));
            //获取整改任务
            List<ParkLotRecordAssignTask> assignTask = parkLotRecordAssignTaskMapper.getAssignTask(auditRecords.getId());
            List<AssignTaskDTO> assignTaskDTOS = BeanMapper.mapList(assignTask, AssignTaskDTO.class);
            for (AssignTaskDTO assignTaskDTO : assignTaskDTOS) {
                List<AssignTaskItemDTO> assignTaskItems = parkLotRecordAssignTaskItemService.getAssignTaskItems(assignTaskDTO.getId());
                //设置整改项文件
                for (AssignTaskItemDTO assignTaskItem : assignTaskItems) {
                    List<Attachment> itemAttachments = attachmentMapper.getAttachments("ParkLotRecordAssignTaskItem", assignTaskItem.getId());
                    assignTaskItem.setAttachments(BeanMapper.mapList(itemAttachments,AttachmentBaseInfoDTO.class));
                }
                //设置整改项
                assignTaskDTO.setAssignments(assignTaskItems);
            }
            auditRecords.setAssignTask(assignTaskDTOS);
        }
        return auditRecordList;
    }

    @Transactional
    @Override
    public void submitAuditRecordDetails(AuditRecordDTO auditRecordDTO) {
        int status = auditRecordDTO.getStatus();
        int stage = auditRecordDTO.getStage();
        int auditStatus = 0;
        //通过
        if (status == 1) {
            switch (stage) {
                case 1:
                    auditStatus = 3;
                    break;
                case 2:
                    auditStatus = 5;
                    break;
                case 3:
                    auditStatus = 7;
                    break;
            }
            handleAuditRecord(auditRecordDTO, auditStatus);
        } else {  //不通过
            switch (stage) {
                case 1:
                    auditStatus = 2;
                    break;
                case 2:
                    auditStatus = 4;
                    break;
                case 3:
                    auditStatus = 6;
                    break;
            }
            handleAuditRecord(auditRecordDTO, auditStatus);
        }
    }

    /**
     * 处理审核任务
     *
     * @param auditRecordDTO
     * @param auditStatus
     */
    public void handleAuditRecord(AuditRecordDTO auditRecordDTO, int auditStatus) {
        int status = auditRecordDTO.getStatus();
        int taskId = auditRecordDTO.getTaskId();
        int stage = auditRecordDTO.getStage();
        //流程履历表添加记录
        ParkLotRecordProcessHistory parkLotRecordProcessHistory = new ParkLotRecordProcessHistory(
                null, taskId, stage, auditRecordDTO.getAuditName(), new Date(), new Date(), status);
        parkLotRecordProcessHistoryMapper.insert(parkLotRecordProcessHistory);
        //审核记录表添加记录
        ParkLotRecordAuditRecord parkLotRecordAuditRecord = new ParkLotRecordAuditRecord(
                taskId, parkLotRecordProcessHistory.getId(), 0, stage, status,
                auditRecordDTO.getAuditorId(), auditRecordDTO.getAuditName(), auditRecordDTO.getAuditReason());
        parkLotRecordAuditRecordMapper.insert(parkLotRecordAuditRecord);

        ParkLotRecordTask parkLotRecordTask = parkLotRecordTaskMapper.selectByPrimaryKey(taskId);
        //发通知
        Format sdf = new SimpleDateFormat("yyyy/MM/dd");
        //获取消息
        String msg = InformUtils.getMsg(stage, status, sdf.format(new Date()), parkLotRecordTask.getName());
        Announcement announcement = new Announcement(taskId, parkLotRecordTask.getUserId(), parkLotRecordTask.getId(), 1, msg);
        announcementMapper.insert(announcement);
        //通过
        if (status == 1) {
            if (stage == 3) {
                //生成备案号
                String recordNo = getRecordNo(parkLotRecordTask);
                parkLotRecordTask.setRecordNo(recordNo);
                parkLotRecordTask.setAuditStatus(auditStatus);
                parkLotRecordTask.setFinishedAt(new Date());
                parkLotRecordTask.setStatus(4);
                //更新备案任务数据
                parkLotRecordTaskMapper.updateByPrimaryKey(parkLotRecordTask);
                ParkLot parkLot = BeanMapper.map(parkLotRecordTask, ParkLot.class);
                //将复审通过后的数据插入停车信息表
                parkLotMapper.insert(parkLot);
                //进入停车场管理列表
            } else {
                //变更审核任务表的审核状态和所在环节
                parkLotRecordTaskMapper.updateAuditAndStage(taskId, stage + 1, auditStatus, 1);
            }
        } else {  //不通过
            if (stage == 3) {
                //退回到现场勘察
                parkLotRecordTaskMapper.updateAuditAndStage(taskId, stage - 1, auditStatus, 2);
            } else {
                //退回经营业主
                parkLotRecordTaskMapper.updateAuditAndStage(taskId, 1, auditStatus, 2);
            }

            //添加整改任务
            for (int i = 0; i < auditRecordDTO.getReformIdeaDTOS().size(); i++) {
                ReformIdeaDTO reformIdeaDTO = auditRecordDTO.getReformIdeaDTOS().get(i);
                ParkLotRecordAssignTask parkLotRecordAssignTask = new ParkLotRecordAssignTask(parkLotRecordAuditRecord.getId(),
                        reformIdeaDTO.getReformIdea());
                parkLotRecordAssignTaskMapper.insert(parkLotRecordAssignTask);
                //关联整改任务附件
                if (reformIdeaDTO.getAttachIds() != null && reformIdeaDTO.getAttachIds().size() != 0) {
                    attachmentMapper.relateAttach(parkLotRecordAssignTask.getId(), TablesName.PARK_LOT_RECORD_ASSIGN_TASK, reformIdeaDTO.getAttachIds());
                }
            }

            //添加备份
            ParkLotRecordTaskLastChange lastChange = BeanMapper.map(parkLotRecordTask, ParkLotRecordTaskLastChange.class);
            lastChange.setAuditStatus(auditStatus);
            lastChange.setParkLotRecordTaskId(taskId);
            parkLotRecordTaskLastChangeMapper.insert(lastChange);
        }
        //关联附件
        if (auditRecordDTO.getAuditResultIds() != null && auditRecordDTO.getAuditResultIds().size() != 0) {
            attachmentMapper.relateAttach(parkLotRecordAuditRecord.getId(), TablesName.PARK_LOT_RECORD_AUDIT_RECORD, auditRecordDTO.getAuditResultIds());
        }
    }

    public String getRecordNo(ParkLotRecordTask parkLotRecordTask) {
        String recordNo = "渝";
        String districtName = parkLotRecordTask.getDistrictName();
        if (districtName.equals("北碚")) {
            recordNo += "碚";
        } else {
            recordNo += districtName.substring(0, 1);
        }
        int parkLotType = parkLotRecordTask.getParkLotType();
        if (parkLotType == 1) {
            recordNo += "配建";
        } else {
            recordNo += "公共";
        }
        int parkLotRommType = parkLotRecordTask.getParkLotRommType();
        if (parkLotRommType == 1) {
            recordNo += "(室内)";
        } else {
            recordNo += "(室外)";
        }
        Format sdf = new SimpleDateFormat("yyyy");
        String format = sdf.format(new Date());
        int timeCount = parkLotRecordTaskMapper.getTimeCount(format);
        recordNo += "-" + format + timeCount;
        return recordNo;
    }
}
